package demo1.demo;

public class Test {
    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(1);
        mySingleList.addLast(2);
        mySingleList.addLast(3);
        mySingleList.addLast(4);
        mySingleList.display3(mySingleList.head);
        System.out.println();
        mySingleList.display4();

    }
    public static void main7(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(2);
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(5);
        myLinkedList.display();
        //myLinkedList.addIndex(5,99);
        //myLinkedList.display();
        System.out.println("=============");
        myLinkedList.removeAllKey(3);
        myLinkedList.display();


    }




    //给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点，返回 null 。
    //相交链表
    public static MySingleList.ListNode getIntersectionNode
            (MySingleList.ListNode headA, MySingleList.ListNode headB) {
        if(headA == null && headB == null) {
            return null;
        }

        //1.分别求出两个链表的长度
        MySingleList.ListNode pl = headA;
        MySingleList.ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        while(pl != null) {
            pl = pl.next;
            lenA++;
        }
        while(ps != null) {
            ps = ps.next;
            lenB++;
        }
        //2.要指回来
        pl = headA;
        ps = headB;
        int length = lenA - lenB;
        //根据length的值修改指想
        if(length < 0) {
            pl = headB;
            ps = headA;
            length = lenB - lenA;
        }
        //1.length一定是一个正数 2.pl指向的链表一定是最长的  ps指向的链表一定是最短的
        while(length != 0) {
            pl = pl.next;
            length--;
        }
        while(pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
//        if(pl == ps && pl != null) {
//            return null;
//        }
        return pl;

    }


    //合并两个有序链表
    public static MySingleList.ListNode mergeTwoLists
            (MySingleList.ListNode list1, MySingleList.ListNode list2) {
        MySingleList.ListNode newHead = new MySingleList.ListNode(0);
        MySingleList.ListNode tmp = newHead;

        while(list1 != null & list2 != null) {
            if(list1.val < list2.val) {
                tmp.next = list1;
                list1 = list1.next;
                tmp = tmp.next;
            }else {
                tmp.next = list2;
                list2 = list2.next;
                tmp = tmp.next;
            }
        }
        if(list1 != null) {
            tmp.next = list1;
        }
        if(list2 != null) {
            tmp.next = list2;
        }
        return newHead.next;
    }

    public static void createCut(MySingleList.ListNode headA, MySingleList.ListNode headB) {
        headB.next.next = headA.next.next;
    }

    public static void main6(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(11);
        mySingleList.addLast(21);
        mySingleList.addLast(31);
        mySingleList.addLast(41);
        mySingleList.display();
        mySingleList.creatLoop();
        boolean ret = mySingleList.hasCycle();
        System.out.println(ret);
        MySingleList.ListNode ret1 = mySingleList.detectCycle();
        System.out.println(ret1.val);

    }
    public static void main5(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(11);
        mySingleList.addLast(21);
        mySingleList.addLast(31);
        mySingleList.addLast(41);
        mySingleList.display();

        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(8);
        mySingleList2.addLast(16);
        mySingleList2.addLast(25);
        mySingleList2.addLast(30);
        mySingleList2.display();

        System.out.println("===============");
        createCut(mySingleList.head,mySingleList2.head);
        MySingleList.ListNode ret = getIntersectionNode(mySingleList.head,mySingleList2.head);
        System.out.println(ret.val);

    }
    public static void main4(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(11);
        mySingleList.addLast(21);
        mySingleList.addLast(31);
        mySingleList.addLast(41);
        mySingleList.display();

        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(8);
        mySingleList2.addLast(16);
        mySingleList2.addLast(25);
        mySingleList2.addLast(30);
        mySingleList2.display();

        System.out.println("===============");
        MySingleList.ListNode ret = mergeTwoLists(mySingleList.head,mySingleList2.head);
        mySingleList.display(ret);
    }
    public static void main3(String[] args) {
        //测试链表逆序
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(1);
        mySingleList.addLast(2);
        mySingleList.addLast(3);
        mySingleList.addLast(4);
        mySingleList.display();
        System.out.println("===========");
//        MySingleList.ListNode ret = mySingleList.reverseList();
//        mySingleList.display(ret);
        MySingleList.ListNode ret = mySingleList.FindKthToTail(3);
        System.out.println(ret.val);

    }
    public static void main1(String[] args) throws InterruptedException {
        MySingleList mySingleList = new MySingleList();
        //mySingleList.creatLink();
        mySingleList.display();
        System.out.println("===========");
        System.out.println(mySingleList.contains(10));
        System.out.println("===========");
        System.out.println(mySingleList.size());
        System.out.println("===试尾插===");
        mySingleList.addLast(28);
        mySingleList.addLast(2);
        mySingleList.addLast(2);
        mySingleList.addLast(4);

        mySingleList.display();
        System.out.println("===试插入===");
        try{
            mySingleList.addIndex(0, 99);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        mySingleList.display();
        System.out.println("===========");
        mySingleList.remove(99);
        mySingleList.display();
        System.out.println("===========");
        mySingleList.removeAllKey(2);
        mySingleList.display();


    }


}
